//
//  RocRemoteControlView.m
//  RocRemoteControlProject
//
//  Created by 新致李照鹏 on 2018/12/18.
//  Copyright © 2018年 Roc. All rights reserved.
//

#import "RocRemoteControlView.h"

@interface RocRemoteControlView ()
{
    CGRect rect_title;
    CGRect rect_home;
    CGRect rect_switch;
    CGRect rect_go;
    CGRect rect_up;
    CGRect rect_down;
    CGRect rect_left;
    CGRect rect_right;
    CGRect rect_before;
    CGRect rect_after;
    CGRect rect_direction;
    CGRect rect_control;
    
}
@property (nonatomic, strong) UIButton *currentButton;

@property (nonatomic, strong) UIImageView *directionIV;

@property (nonatomic, assign) CGFloat lastValue;

@property (nonatomic, assign) BOOL is_RunMode;

@end

@implementation RocRemoteControlView

#pragma mark - 界面逻辑
/**
  按钮按下
  当点击时，立即传指令给设备端。对应的指令：前后左右forward | backoff | leftturn | rightturn
  当长按时， 按下>300 ms,发送repeat指令，松开发送repeat结束指令。
  长按对应指令：按下 forwardRepStart | backoffRepStart | leftturnRepStart | rightturnRepStart; 松开 forwardRepStop | backoffRepStop | leftturnRepStop | rightturnRepStop
  按下按钮时，机器人前方的指路箭头会变成对应的。机器人切换成走路模式（简单的晃一下，发出点效果）。
  按开始狂奔，发送指令： runStart ，按钮切换成立刻结束。点击立刻结束：发送runStop，按钮切换成开始狂奔。
 */
- (void)toDo_ButtonTouchDown:(UIButton *)button Type:(RocRemoteControlViewButtonType)type
{
    if (self.currentButton == nil || self.currentButton != button) {
        
        if (self.is_RunMode) {
            self.currentButton.selected = NO;
            button.selected = YES;
        }else{
            self.currentButton.selected = NO;
        }
        
        
        [self animationWhenTouchButton:type];
        
        
        self.currentButton = button;
    }
}
/**
 按钮收起
 */
- (void)toDo_ButtonTouchUp:(UIButton *)button Type:(RocRemoteControlViewButtonType)type
{
    if (self.is_RunMode) {
        if (button != self.currentButton ) {
            button.selected = NO;
        }else{
            
        }
    }else{
        if (self.currentButton && self.currentButton == button) {
            self.currentButton.selected = NO;
            self.currentButton = nil;
        }
    }
}

#pragma mark - 暂时添加 后续会移走
// 添加定时器
- (void)addTimer
{
    NSTimer *timer ;
}
- (void)startTimer
{
    
}
- (void)stopTimer
{
    
}
- (void)endTimer
{
    
}

#pragma mark - 动画
- (void)animationWhenTouchButton:(RocRemoteControlViewButtonType)type
{
    CGFloat toValue = 0.0;
    if (type == RocRemoteControlViewButtonUp) { // 上
        toValue = 0.0;
    }else if (type == RocRemoteControlViewButtonDown) { // 下
        toValue = 1*M_PI;
    }else if (type == RocRemoteControlViewButtonLeft) { // 左
        toValue = -0.5*M_PI;
    }else { // 右
        toValue = 0.5*M_PI;
    }
    
    CABasicAnimation *basicAnim = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    basicAnim.fromValue = @(self.lastValue);
    basicAnim.toValue = @(toValue);
    basicAnim.duration = 0.2f;// 单次动画执行的时间
    basicAnim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
//    basicAnim.repeatCount = HUGE_VAL; // 动画执行次数
//    basicAnim.autoreverses = YES; // 反向再执行一次
    // 以下两句 控制View动画结束后，停留在动画结束的位置
    basicAnim.removedOnCompletion = NO;
    basicAnim.fillMode = kCAFillModeForwards;  // 停留在动画最后一帧
    [self.directionIV.layer addAnimation:basicAnim forKey:@"transform_rotation_z"];
    
    self.lastValue = toValue;
}


#pragma mark - timer

#pragma mark - 视图创建
/**
 视图初始化
 */
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        rect_title = KROCConver_Rect(925, 43, 370, 75);
        rect_home = KROCConver_Rect(69, 118, 176, 135);
        rect_switch = KROCConver_Rect(1981, 118, 170, 135);
        rect_go = KROCConver_Rect(885, 1065, 411, 108);

        rect_up = KROCConver_Rect(89, 467, 216, 191);
        rect_down = KROCConver_Rect(89, 807, 216, 185);
        rect_left = KROCConver_Rect(1926, 467, 224, 191);
        rect_right = KROCConver_Rect(1926, 807, 224, 185);
        
        rect_before = KROCConver_Rect(457, 219, 272, 291);
        rect_after = KROCConver_Rect(1519, 219, 272, 291);
        rect_direction = KROCConver_Rect(981, 189, 278, 278);
        rect_control = KROCConver_Rect(885, 497, 454, 482);
        
        self.currentButton = nil;
        self.lastValue = 0.0;
        self.is_RunMode = NO;
        
        
        [self buildBGImageView];
        
        [self buildTitle];
        [self buildHome];
        [self buildSwitchModel];
        [self buildGo];
        
        [self buildUp];
        [self buildDown];
        [self buildLeft];
        [self buildRight];
        
        [self buildBefore];
        [self buildAfter];
        [self buildDirection];
        [self buildControl];
    }
    return self;
}

/**
 *  背景图
 */
- (void)buildBGImageView
{
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:self.bounds];
    bgImageView.contentMode = UIViewContentModeScaleToFill;
    bgImageView.image = [UIImage imageNamed:@"controlModeCut.png"];
    [self addSubview:bgImageView];
}

/**
 *  标题图
 */
- (void)buildTitle
{
    //controlModeCut_03
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:rect_title];
    imageView.contentMode = UIViewContentModeScaleToFill;
    imageView.image = [UIImage imageNamed:@"controlModeCut_03.png"];
    [self addSubview:imageView];
}

/**
 *  主页图
 */
- (void)buildHome
{
    //controlModeCut_06
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:rect_home];
    imageView.contentMode = UIViewContentModeScaleToFill;
    imageView.image = [UIImage imageNamed:@"controlModeCut_06.png"];
    [self addSubview:imageView];
}

/**
 *  切换图
 */
- (void)buildSwitchModel
{
    //controlModeCut_08
    UIImage *image = [UIImage imageNamed:@"controlModeCut_08.png"];
    
    UIButton *button = [[UIButton alloc] initWithFrame:rect_switch];
    [button setImage:image forState:UIControlStateNormal];
    [self addSubview:button];
    
    [button addTarget:self action:@selector(buttonActionSwitch) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonActionSwitch
{
    if (self.mydelegate && [self.mydelegate respondsToSelector:@selector(RocRemoteControlView:ClickButton:)]) {
        [self.mydelegate RocRemoteControlView:self ClickButton:RocRemoteControlViewButtonSwitch];
    }
}

/**
 *  开始图
 */
- (void)buildGo
{
    //controlModeCut_43c 狂奔模式  controlModeCut_43d 立刻结束
    UIImage *image = [UIImage imageNamed:@"controlModeCut_43c.png"];
    UIImage *imageH = [UIImage imageNamed:@"controlModeCut_43d.png"];
    
    UIButton *button = [[UIButton alloc] initWithFrame:rect_go];
    [button setImage:image forState:UIControlStateNormal];
    [button setImage:imageH forState:UIControlStateSelected];
    [self addSubview:button];
    
    [button addTarget:self action:@selector(buttonActionGo:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonActionGo:(UIButton *)sender
{
    sender.selected = !sender.selected;
    self.is_RunMode = sender.selected;
    if (self.currentButton) {
        if (self.is_RunMode) {
            self.currentButton.selected = YES;
        }else{
            self.currentButton.selected = NO;
            self.currentButton = nil;
        }
    }
    
    
}

/**
 *  上
 */
- (void)buildUp
{
    //controlModeCut_24 controlModeCut_24b
    UIImage *image = [UIImage imageNamed:@"controlModeCut_24.png"];
    UIImage *imageH = [UIImage imageNamed:@"controlModeCut_24b.png"];
    
    UIButton *button = [[UIButton alloc] initWithFrame:rect_up];
    [button setImage:image forState:UIControlStateNormal];
    [button setImage:imageH forState:UIControlStateHighlighted];
    [button setImage:imageH forState:UIControlStateSelected];
    [self addSubview:button];
    
    [button addTarget:self action:@selector(buttonActionUp_EventUp:) forControlEvents:UIControlEventTouchUpInside];
    [button addTarget:self action:@selector(buttonActionUp_EventUp:) forControlEvents:UIControlEventTouchUpOutside];
    [button addTarget:self action:@selector(buttonActionUp_EventDown:) forControlEvents:UIControlEventTouchDown];
}
- (void)buttonActionUp_EventDown:(UIButton *)sender
{
    [self toDo_ButtonTouchDown:sender Type:RocRemoteControlViewButtonUp];
}
- (void)buttonActionUp_EventUp:(UIButton *)sender
{
    [self toDo_ButtonTouchUp:sender Type:RocRemoteControlViewButtonUp];
}
/**
 *  下
 */
- (void)buildDown
{
    //controlModeCut_38 controlModeCut_38
    UIImage *image = [UIImage imageNamed:@"controlModeCut_38.png"];
    UIImage *imageH = [UIImage imageNamed:@"controlModeCut_38b.png"];
    
    UIButton *button = [[UIButton alloc] initWithFrame:rect_down];
    [button setImage:image forState:UIControlStateNormal];
    [button setImage:imageH forState:UIControlStateHighlighted];
    [button setImage:imageH forState:UIControlStateSelected];
    [self addSubview:button];
    
    [button addTarget:self action:@selector(buttonActionDown_EventUp:) forControlEvents:UIControlEventTouchUpInside];
    [button addTarget:self action:@selector(buttonActionDown_EventUp:) forControlEvents:UIControlEventTouchUpOutside];
    [button addTarget:self action:@selector(buttonActionDown_EventDown:) forControlEvents:UIControlEventTouchDown];
}
- (void)buttonActionDown_EventDown:(UIButton *)sender
{
    [self toDo_ButtonTouchDown:sender Type:RocRemoteControlViewButtonDown];
}
- (void)buttonActionDown_EventUp:(UIButton *)sender
{
    [self toDo_ButtonTouchUp:sender Type:RocRemoteControlViewButtonDown];
}

/**
 *  左
 */
- (void)buildLeft
{
    //controlModeCut_22 controlModeCut_22b
    UIImage *image = [UIImage imageNamed:@"controlModeCut_22.png"];
    UIImage *imageH = [UIImage imageNamed:@"controlModeCut_22b.png"];
    
    UIButton *button = [[UIButton alloc] initWithFrame:rect_left];
    [button setImage:image forState:UIControlStateNormal];
    [button setImage:imageH forState:UIControlStateHighlighted];
    [button setImage:imageH forState:UIControlStateSelected];
    [self addSubview:button];
    
    [button addTarget:self action:@selector(buttonActionLeft_EventUp:) forControlEvents:UIControlEventTouchUpInside];
    [button addTarget:self action:@selector(buttonActionLeft_EventUp:) forControlEvents:UIControlEventTouchUpOutside];
    [button addTarget:self action:@selector(buttonActionLeft_EventDown:) forControlEvents:UIControlEventTouchDown];
}
- (void)buttonActionLeft_EventDown:(UIButton *)sender
{
    [self toDo_ButtonTouchDown:sender Type:RocRemoteControlViewButtonLeft];
}
- (void)buttonActionLeft_EventUp:(UIButton *)sender
{
    [self toDo_ButtonTouchUp:sender Type:RocRemoteControlViewButtonLeft];
}

/**
 * 右
 */
- (void)buildRight
{
    //controlModeCut_35 controlModeCut_35b
    UIImage *image = [UIImage imageNamed:@"controlModeCut_35.png"];
    UIImage *imageH = [UIImage imageNamed:@"controlModeCut_35b.png"];
    
    UIButton *button = [[UIButton alloc] initWithFrame:rect_right];
    [button setImage:image forState:UIControlStateNormal];
    [button setImage:imageH forState:UIControlStateHighlighted];
    [button setImage:imageH forState:UIControlStateSelected];
    [self addSubview:button];
    
    [button addTarget:self action:@selector(buttonActionRight_EventUp:) forControlEvents:UIControlEventTouchUpInside];
    [button addTarget:self action:@selector(buttonActionRight_EventUp:) forControlEvents:UIControlEventTouchUpOutside];
    [button addTarget:self action:@selector(buttonActionRight_EventDown:) forControlEvents:UIControlEventTouchDown];
}
- (void)buttonActionRight_EventDown:(UIButton *)sender
{
    [self toDo_ButtonTouchDown:sender Type:RocRemoteControlViewButtonRight];
}
- (void)buttonActionRight_EventUp:(UIButton *)sender
{
    [self toDo_ButtonTouchUp:sender Type:RocRemoteControlViewButtonRight];
}

/**
 *  前
 */
- (void)buildBefore
{
    //controlModeCut_14
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:rect_before];
    imageView.contentMode = UIViewContentModeScaleToFill;
    imageView.image = [UIImage imageNamed:@"controlModeCut_14.png"];
    [self addSubview:imageView];
}
/**
 *  后
 */
- (void)buildAfter
{
    //controlModeCut_17
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:rect_after];
    imageView.contentMode = UIViewContentModeScaleToFill;
    imageView.image = [UIImage imageNamed:@"controlModeCut_17.png"];
    [self addSubview:imageView];
}
/**
 *  方向
 */
- (void)buildDirection
{
    //controlModeCut_11
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:rect_direction];
    imageView.contentMode = UIViewContentModeScaleToFill;
    imageView.image = [UIImage imageNamed:@"controlModeCut_11.png"];
    [self addSubview:imageView];
    
    self.directionIV = imageView;
}
/**
 *  控制盘
 */
- (void)buildControl
{
    //controlModeCut_10-28
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:rect_control];
    imageView.contentMode = UIViewContentModeScaleToFill;
    imageView.image = [UIImage imageNamed:@"controlModeCut_10-28.png"];
    [self addSubview:imageView];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
